package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.CommentEvaluationMapper;
import com.qingcheng.dao.CommentLogMapper;
import com.qingcheng.dao.CommentMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.pojo.comment.CommentEvaluation;
import com.qingcheng.pojo.comment.CommentLog;
import com.qingcheng.service.comment.CommentService;

import com.qingcheng.util.IdWorker;
import com.qingcheng.util.SensitiveWord;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImplHt implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentEvaluationMapper commentEvaluationMapper;

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CommentLogMapper commentLogMapper;

    @Autowired
    private SensitiveWord sensitiveWord;
    /**
     * 返回全部记录
     *
     * @return
     */
    public List<Comment> findAll() {
        return commentMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Comment> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Comment> comments = (Page<Comment>) commentMapper.selectAll();
        return new PageResult<Comment>(comments.getTotal(), comments.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<Comment> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return commentMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Comment> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Comment> comments = (Page<Comment>) commentMapper.selectByExample(example);
        return new PageResult<Comment>(comments.getTotal(), comments.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public Comment findById(byte[] id) {
        return commentMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param comment
     */
    public void add(Comment comment) {
        commentMapper.insert(comment);
    }

    /**
     * 修改
     *
     * @param comment
     */
    public void update(Comment comment) {

        Comment comment1 = new Comment();
        comment1.setId(comment.getId());
        comment1.setIsDelete(comment.getIsDelete());
        commentMapper.updateByPrimaryKeySelective(comment1);
        //添加操作日志
        CommentLog commentLog = new CommentLog();
        commentLog.setId(idWorker.nextId()+"");
        commentLog.setCommentId(comment.getId());
        commentLog.setOperator("admin");
        commentLog.setOperateTime(new Date());
        commentLog.setUpdateTime(new Date());
        commentLog.setCommentStatus(comment.getIsDelete().toString());
        commentLog.setRemarks("备注信息");
        commentLogMapper.insert(commentLog);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(byte[] id) {
        commentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Comment> findByUsername(String username) {

        return commentMapper.findListByUsername(username);
    }

    /**
     *过滤敏感评论
     * @param comment
     */
    @Override
    public String sensitiveWordmask(Comment comment) {
        long startNumer = System.currentTimeMillis();
        sensitiveWord=new SensitiveWord("CensorWords.txt");
        sensitiveWord.InitializationWork();
        String str = comment.getMessage();

        str = sensitiveWord.filterInfo(str);
        long endNumber = System.currentTimeMillis();

        return str;
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 用户名
            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andLike("username", "%" + searchMap.get("username") + "%");
            }
            // 订单id
            if (searchMap.get("orderId") != null && !"".equals(searchMap.get("orderId"))) {
                criteria.andLike("orderId", "%" + searchMap.get("orderId") + "%");
            }
            // 商品id
            if (searchMap.get("spuId") != null && !"".equals(searchMap.get("spuId"))) {
                criteria.andLike("spuId", "%" + searchMap.get("spuId") + "%");
            }
            // 图片地址
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 评论内容
            if (searchMap.get("message") != null && !"".equals(searchMap.get("message"))) {
                criteria.andLike("message", "%" + searchMap.get("message") + "%");
            }
            // 追评
            if (searchMap.get("response") != null && !"".equals(searchMap.get("response"))) {
                criteria.andLike("response", "%" + searchMap.get("response") + "%");
            }
            // 追评图片
            if (searchMap.get("responseImage") != null && !"".equals(searchMap.get("responseImage"))) {
                criteria.andLike("responseImage", "%" + searchMap.get("responseImage") + "%");
            }
            // 商品规格
            if (searchMap.get("spuSpec") != null && !"".equals(searchMap.get("spuSpec"))) {
                criteria.andLike("spuSpec", "%" + searchMap.get("spuSpec") + "%");
            }

            // 评级  1 差评 2-3中评  4-5 好评
            if (searchMap.get("star") != null) {
                criteria.andEqualTo("star", searchMap.get("star"));
            }
            // 点赞数
            if (searchMap.get("greatCount") != null) {
                criteria.andEqualTo("greatCount", searchMap.get("greatCount"));
            }
            // 0:未删除  1：已删除
            if (searchMap.get("isDelete") != null) {
                criteria.andEqualTo("isDelete", searchMap.get("isDelete"));
            }
            // 0:未举报  1:已举报  2:审核通过
            if (searchMap.get("reportStatus") != null) {
                criteria.andEqualTo("reportStatus", searchMap.get("reportStatus"));
            }

        }
        return example;
    }

}
